
package com.epam.video.test;

import com.epam.video.entities.Si;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

/**
 * TODO: add description.
 * 
 * @author slava
 */
public class InteractionActivity extends Activity {
  public static final String INTERACTION_ITEM_EXTRA = "interaction_item_extra";

  private Button selectButton;

  private ListView listView;

  private ArrayAdapter<String> listViewAdapter;

  private Si siItem;

  private int checkedPosition = -1;

  @Override
  public Object onRetainNonConfigurationInstance() {
    return checkedPosition;
  }

  @Override
  protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.interaction_layout);
    initListView();
    initSelectAnswerButton();
    checkStoredPosition();

    siItem = (Si) getIntent().getSerializableExtra(INTERACTION_ITEM_EXTRA);
    initData(siItem);
  }

  private void initListView() {
    listViewAdapter = new ArrayAdapter<String>(this, R.layout.interaction_answer_layout);
    listView = (ListView) findViewById(R.id.interaction_answers_listview);
    listView.setAdapter(listViewAdapter);
    listView.setItemsCanFocus(false);
    listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    listView.setOnItemClickListener(new OnItemClickListener() {
      @Override
      public void onItemClick(final AdapterView<?> parent, final View view, final int position, final long id) {
        checkedPosition = position;
      }
    });
  }

  private void checkStoredPosition() {
    Integer storedPosition = (Integer) getLastNonConfigurationInstance();
    if (storedPosition != null) {
      checkedPosition = storedPosition.intValue();
    }
  }

  private void initSelectAnswerButton() {
    selectButton = (Button) findViewById(R.id.select_answer);
    selectButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(final View v) {
        handleAnswer();
      }
    });
  }

  private void handleAnswer() {

    int checkedPosition = listView.getCheckedItemPosition();
    if (checkedPosition >= 0 && checkedPosition < siItem.getAnswers().size()) {

      if (checkedPosition == 0) {
        goToAnotherSi();
      } else {
        siItem.getAnswers().get(checkedPosition);
      }

      // add aditional logic and finish this activity.

    }
  }

  private void goToAnotherSi() {
    String siInJson = Config.instance().getSi2Json();
    Si siItem = JsonParserUtils.parseSi(siInJson);
    initData(siItem);
  }

  private void initData(final Si siItem) {
    listViewAdapter.clear();

    if (siItem != null) {
      String question = siItem.getQuestion();
      ((TextView) findViewById(R.id.interaction_title)).setText(question);

      List<String> answers = siItem.getAnswers();
      for (String answer : answers) {
        listViewAdapter.add(answer);

      }
      if (checkedPosition >= 0) {
        listView.setSelection(checkedPosition);
      }
    }
    listView.invalidate();
  }
}
